<?php

class Admin_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

	public function indexAction() {
	    
	}
	public function preDispatch() {
		
	    $auth = Zend_Auth::getInstance();
	    if (!$auth->hasIdentity()) {
	        if ($this->_request->getActionName() != 'login') {
	            $this->_redirect('/admin/index/login');
	        }
	    }
	}
	public function loginAction() {
		$this->_helper->layout->disableLayout();
		$mess='';
	    if ($this->_request->isPost()) {
	        //1.Goi ket noi voi Zend Db
	       
	       
	        //$db = Zend_Db::factory($dbOption['adapter'],$dbOption['params']);
	        //2. Khoi tao Zend Autho
	        $auth = Zend_Auth::getInstance ();		
	        //3. Khai bao bang va 2 cot se su dung so sanh trong qua tronh login
	        $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table_Abstract::getDefaultAdapter());
	        $authAdapter->setTableName('hae_user')
	                ->setIdentityColumn('username')
	                ->setCredentialColumn('password');
	
	        //4. Lay gia tri duoc gui qua tu FORM
	        $uname = $this->_request->getParam('username');
	        $paswd = $this->_request->getParam('password');
			
	        //5. Dua vao so sanh voi du lieu khai bao o muc 3
	        $authAdapter->setIdentity($uname);
	        $authAdapter->setCredential(md5($paswd));
	
	        //6. Kiem tra trang thai cua user neu status = 1 moi duoc login
	        $select = $authAdapter->getDbSelect();
	        $select->where('status = 1');
	
	        //7. Lay ket qua truy van
	        $result = $auth->authenticate($authAdapter);
	        $flag = false;
	       
	        if ($result->isValid()) {
	            //8. Lay nhung du lieu can thiet trong bang users neu login thanh cong				
	            $data = $authAdapter->getResultRowObject(null, array('password'));
	
	            //9. Luu  nhung du lieu cua member vao session
	            $auth->getStorage()->write($data);
	            $flag = true;
	        }
	        if($flag == false)
	        {
	        	$mess='Username or Password is not correct ';
	        }
	        if ($flag == true) {
	            $this->_redirect('/admin/index');
	        }
	    }
	    $this->view->mess=$mess;
	}
	public function logoutAction() {
	    $auth = Zend_Auth::getInstance();
	    $auth->clearIdentity();
	    $this->_redirect('/index');
	    
	}

}

